package com.hbx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbx.constant.CommonContent;
import com.hbx.dto.BaseRequest;
import com.hbx.dto.rank.InsertOrUpdateDto;
import com.hbx.exception.RspException;
import com.hbx.model.Rank;
import com.hbx.mapper.RankMapper;
import com.hbx.model.UserRank;
import com.hbx.service.RankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.service.UserRankService;
import com.hbx.utils.Result;
import com.hbx.vo.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbx
 * @since 2022-01-15
 */
@Service
@Slf4j
public class RankServiceImpl extends ServiceImpl<RankMapper, Rank> implements RankService {

    @Autowired
    private RankMapper rankMapper;

    @Autowired
    private UserRankService userRankService;

    /**
     * 根据用户名获取头衔
     * @param userId
     * @return
     */
    @Override
    public List<Rank> getRankList(Integer userId) {
        if (ObjectUtils.isEmpty(userId)){
            log.info("【头衔查询】:用户id为null,查询失败");
            return null;
        }
        List<Rank> rankList = rankMapper.selectListByUserId(userId);
        log.info("【头衔查询】:用户头衔获取成功,共{}个",rankList.size());
        return rankList;
    }

    /**
     * 获得头衔列表
     * @param request
     * @param r
     */
    @Override
    public void getRankListPage(BaseRequest request, Result r) {
        PageBean pageBean = new PageBean();
        Page<Rank> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<Rank> rankIPage = rankMapper.selectPage(page, null);
        pageBean.setTotal(rankIPage.getTotal());
        pageBean.setPageNum(rankIPage.getCurrent());
        pageBean.setPageSize(rankIPage.getSize());
        pageBean.setData(rankIPage.getRecords());
        r.setCode(200).setData(pageBean);
    }

    /**
     * 删除头衔
     * @param id
     * @param r
     */
    @Override
    @Transactional
    public void removeRank(Integer id, Result r) {
        if (id == 1){
            r.setCode(201).setMsg("暂无权限");
            return;
        }
        // 删除已经授予的头衔
        QueryWrapper<UserRank> wrapper = new QueryWrapper<>();
        wrapper.eq("rank_id",id);
        if (userRankService.remove(wrapper)){
            int result = rankMapper.deleteById(id);
            if (result != 1){
                throw new RspException("头衔删除失败");
            }
            r.setCode(200).setMsg("头衔删除成功");
        }else {
            throw new RspException("头衔删除失败");
        }
    }

    /**
     *添加或修改
     * @param dto
     * @param r
     */
    @Override
    public void insertOrUpdateRank(InsertOrUpdateDto dto, Result r) {
        String rankName = dto.getRankName();
        if (Arrays.asList(CommonContent.BAN_RANK).contains(rankName.trim())){
            r.setCode(201).setMsg("头衔包含敏感词");
            return;
        }
        Integer id = dto.getId();
        Rank rank = new Rank();
        BeanUtils.copyProperties(dto, rank);
        Integer result = 0;
        if (id == null){
            // 添加逻辑
            QueryWrapper<Rank> wrapper = new QueryWrapper<>();
            wrapper.eq("rank_name",rankName);
            List<Rank> ranks = rankMapper.selectList(wrapper);
            if (!CollectionUtils.isEmpty(ranks)){
                r.setCode(201).setMsg("标签名已存在");
                return;
            }
            result = rankMapper.insert(rank);
        } else{
            result = rankMapper.updateById(rank);
        }

        if (result != 1){
            r.setCode(201).setMsg("操作失败");
            return;
        }
        r.setCode(200).setMsg("操作成功");
    }
}
